home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dlink / subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-09  |  8.4 KB  |  474 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  SUBS.C
  9.  *
  10.  */
  11.  
  12. /*
  13. **      $Filename: subs.c $
  14. **      $Author: dice $
  15. **      $Revision: 30.326 $
  16. **      $Date: 1995/12/24 06:10:40 $
  17. **      $Log: subs.c,v $
  18.  * Revision 30.326  1995/12/24  06:10:40  dice
  19.  * .
  20.  *
  21.  * Revision 30.5  1994/06/13  18:38:38  dice
  22.  * removed assignment prefix to include for unix portability
  23.  * byte ordering portability
  24.  *
  25.  * Revision 30.0  1994/06/10  18:05:40  dice
  26.  * .
  27.  *
  28.  * Revision 1.3  1993/10/13  19:52:19  jtoebes
  29.  * Fix warning message about pointer mismatch.
  30.  *
  31.  * Revision 1.2  1993/10/13  19:48:28  jtoebes
  32.  * FIX BUG01046 - Dlink should look in DCC_CONFIG: for config files.
  33.  *
  34. **/
  35.  
  36.  
  37. #include "defs.h"
  38. #include <fcntl.h>
  39.  
  40. Prototype long      MemMalloced;
  41. Prototype long      MemAllocated;
  42. Prototype long      MemRequested;
  43. Prototype long      MemReclaimed;
  44. Prototype long      MemNumSyms;
  45. Prototype long      MemNumHunks;
  46. Prototype long      MemNumModules;
  47. Prototype long      MemNumHunksMalReclaim;
  48. Prototype long      MemNumSymsMalReclaim;
  49.  
  50. Prototype void    cerror(short, ...);
  51. Prototype void    _Assert(char *, int);
  52. Prototype void    *zalloc(long);
  53. Prototype void    zfree(void *, long);
  54. Prototype void    putl(FILE *, long);
  55. Prototype char    *HunkToStr(Hunk *);
  56. Prototype void _SanityCheck(int);
  57. Prototype int  open_lpath(char *, long);
  58. Prototype Node *MakeNode(char *);
  59. Prototype Node *MakeNode2(char *, char *);
  60. Prototype char *ObtainErrorString(short);
  61. Prototype void NoMemory(void);
  62.  
  63. Prototype void eprintf(const char *ctl, ...);
  64. Prototype void veprintf(const char *ctl, va_list va);
  65. Prototype void eputc(char c);
  66.  
  67. void *SetRequester(void *);
  68.  
  69. char    *ErrorFileName1 = DCC_CONFIG "dice.errors";
  70. char    *ErrorFileName2 = DCC "config/dice.errors";
  71. char    *ErrorAry;
  72. long    ErrorArySize;
  73. char    ErrBuf[128];
  74.  
  75.  
  76. long    MemMalloced;
  77. long    MemAllocated;
  78. long    MemRequested;
  79. long    MemReclaimed;
  80. long    MemNumSyms;
  81. long    MemNumHunks;
  82. long    MemNumModules;
  83. long    MemNumHunksMalReclaim;
  84. long    MemNumSymsMalReclaim;
  85.  
  86. #define BLOCK_SIZE  4096
  87. #define MAX_FREE    256
  88.  
  89. long **FreeList[MAX_FREE/4];
  90.  
  91. void
  92. cerror(short errorId, ...)
  93. {
  94.     static const char *Ary[] = {
  95.     "?Bad", NULL, "Warning", "Error", "SoftError", "Fatal"
  96.     };
  97.     va_list va;
  98.  
  99.     if (Ary[errorId >> 12]) {
  100.     eprintf("DLINK: \"%s\" L:0 C:0 %.*s:%d ",
  101.         "",
  102.         ((ErrorOpt == 2) ? 1 : (int)strlen(Ary[errorId >> 12])),
  103.         Ary[errorId >> 12],
  104.         errorId & 0x0FFF
  105.     );
  106.     }
  107.  
  108.     va_start(va, errorId);
  109.     veprintf(ObtainErrorString(errorId & 0x0FFF), va);
  110.     va_end(va);
  111.     eputc('\n');
  112.  
  113.     switch(errorId & EF_MASK) {
  114.     case EF_WARN:
  115.     if (ExitCode < 5)
  116.         ExitCode = 5;
  117.     break;
  118.     case EF_FATAL:
  119.     case EF_SOFT:
  120.     xexit(20);
  121.     break;
  122.     case EF_ERROR:
  123.     if (ExitCode < 20)
  124.         ExitCode = 20;
  125.     break;
  126.     }
  127. }
  128.  
  129. void
  130. NoMemory()
  131. {
  132.     eprintf("NO MEMORY!\n");
  133.     xexit(25);
  134. }
  135.  
  136. void        
  137. eprintf(const char *ctl, ...)
  138. {   
  139.     va_list va;
  140.     
  141.     va_start(va, ctl);
  142.     veprintf(ctl, va);
  143.     va_end(va); 
  144. }
  145.     
  146. void    
  147. veprintf(const char *ctl, va_list va)
  148. {   
  149.     vfprintf(stderr, ctl, va);
  150.     if (ErrorFi)
  151.         vfprintf(ErrorFi, ctl, va);
  152. }           
  153.  
  154. void
  155. eputc(char c)
  156. {
  157.     fputc(c, stderr);
  158.     if (ErrorFi)
  159.         fputc(c, ErrorFi);
  160. }
  161.  
  162.  
  163. void
  164. _Assert(file, line)
  165. char *file;
  166. int line;
  167. {
  168.     cerror(EFATAL_ASSERT, line, file);
  169. }
  170.  
  171. void *
  172. zalloc(bytes)
  173. long bytes;
  174. {
  175.     void *ptr;
  176.     static char *BufPtr1;
  177.     static char *BufPtr2;
  178.     static long BufLen1;
  179.     static long BufLen2;
  180.  
  181.     if (bytes == 0)
  182.     return((void *)"");
  183.  
  184.     bytes = (bytes + 7) & ~3;
  185.     MemRequested += bytes;
  186.     if (bytes < MAX_FREE) {
  187.     long index = bytes >> 2;
  188.  
  189.     if ((ptr = FreeList[index]) != NULL) {
  190.         FreeList[index] = (long **)*(long *)ptr;
  191.         setmem(ptr, bytes, 0);
  192.         return(ptr);
  193.     }
  194.     }
  195.     MemAllocated += bytes;
  196.  
  197.     if (bytes <= BufLen1) {
  198.     ptr = BufPtr1;
  199.     BufPtr1 += bytes;
  200.     BufLen1 -= bytes;
  201.     if (BufLen1 == 0) {
  202.         BufLen1 = BufLen2;
  203.         BufPtr1 = BufPtr2;
  204.         BufLen2 = 0;
  205.     }
  206.     return(ptr);
  207.     }
  208.     if (bytes <= BufLen2) {
  209.     ptr = BufPtr2;
  210.     BufPtr2 += bytes;
  211.     BufLen2 -= bytes;
  212.     return(ptr);
  213.     }
  214.     if (bytes > BLOCK_SIZE/4) {
  215.     ptr = malloc(bytes);
  216.     setmem(ptr, bytes, 0);
  217.     return(ptr);
  218.     }
  219.     if (BufLen2) {
  220.     BufLen1 = BufLen2;
  221.     BufPtr1 = BufPtr2;
  222.     }
  223.     ptr = malloc(BLOCK_SIZE);
  224.     if (ptr == NULL)
  225.     NoMemory();
  226.     setmem(ptr, BLOCK_SIZE, 0);
  227.  
  228.     BufPtr2 = (char *)ptr + bytes;
  229.     BufLen2 = BLOCK_SIZE - bytes;
  230.  
  231.     return(ptr);
  232. }
  233.  
  234. void
  235. zfree(ptr, bytes)
  236. void *ptr;
  237. long bytes;
  238. {
  239.     long index = (bytes + 7) >> 2;
  240.  
  241.     if (bytes == 0) {
  242.     cerror(EFATAL_MEMORY_FREE_ERROR);
  243.     }
  244.     if (index < MAX_FREE/4) {
  245.     *(long *)ptr = (long)FreeList[index];
  246.     FreeList[index] = (long **)ptr;
  247.     MemReclaimed += index << 2;
  248.     }
  249. }
  250.  
  251. void
  252. putl(fo, n)
  253. long n;
  254. FILE *fo;
  255. {
  256.     n = ToMsbOrder(n);
  257.     fwrite((char *)&n, sizeof(long), 1, fo);
  258. }
  259.  
  260. char *
  261. HunkToStr(hunk)
  262. Hunk *hunk;
  263. {
  264.     static char Buf[2][256];
  265.     static int Which;
  266.     char *ptr = Buf[Which];
  267.  
  268.     Which = 1 - Which;
  269.     if (hunk == NULL) {
  270.     sprintf(ptr, "[NULL]");
  271.     return(ptr);
  272.     }
  273.     sprintf(ptr, "[%s:%s hn#%d.%ld off %ld siz %ld]", hunk->Module->FNode->Node.ln_Name, hunk->Module->Node.ln_Name, hunk->HunkNo, hunk->HX->FinalHunkNo, hunk->Offset, hunk->Bytes);
  274.     return(ptr);
  275. }
  276.  
  277. #ifdef SANITY_CHECK
  278.  
  279. void
  280. _SanityCheck(n)
  281. {
  282.     static short x = 0;
  283.     static short y = 1085;
  284.  
  285.     Forbid();
  286.     AvailMem(MEMF_LARGEST);
  287.     Permit();
  288.     if (y)
  289.     --y;
  290.     else
  291.     Delay(50);
  292.     printf("%d %d\n", x++, n);
  293. }
  294.  
  295. #endif
  296.  
  297. /*
  298.  *  find and open a file
  299.  */
  300.  
  301. int
  302. open_lpath(name, modes)
  303. char *name;
  304. long modes;
  305. {
  306.     int fd = -1;
  307.     short fullPath = (strchr(name, ':') ? 1 : 0);
  308.     Node *node;
  309.  
  310.     Tmp[0] = 0;
  311.     for (node = GetHead(&LibDirList); node; node = GetSucc(node)) {
  312.     sprintf(Tmp, "%s%s", node->ln_Name, name);
  313.     fd = open(Tmp, modes);
  314.     if (fd >= 0 || fullPath)
  315.         break;
  316.     }
  317.     if (fd < 0 && PostFix[0]) {
  318.     for (node = GetHead(&LibDirList); node; node = GetSucc(node)) {
  319.         char *ptr;
  320.  
  321.         sprintf(Tmp, "%s%s", node->ln_Name, name);
  322.         if ((ptr = strstr(Tmp, ".lib")) != NULL)
  323.         strins(ptr, PostFix);
  324.         else
  325.         strcat(Tmp, PostFix);
  326.  
  327.         fd = open(Tmp, modes);
  328.         if (fd >= 0 || fullPath)
  329.         break;
  330.     }
  331.     }
  332.     if (fd < 0)
  333.     cerror(EERROR_CANT_FIND_LIB, name, PostFix);
  334.     return(fd);
  335. }
  336.  
  337. Node *
  338. MakeNode(ptr)
  339. char *ptr;
  340. {
  341.     return(MakeNode2(ptr, ""));
  342. }
  343.  
  344. Node *
  345. MakeNode2(p1, p2)
  346. char *p1;
  347. char *p2;
  348. {
  349.     Node *node = malloc(sizeof(Node) + strlen(p1) + strlen(p2) + 1);
  350.  
  351.     node->ln_Name = (char *)(node + 1);
  352.     strcpy(node->ln_Name, p1);
  353.     strcat(node->ln_Name, p2);
  354.     return(node);
  355. }
  356.  
  357.  
  358. char *
  359. ObtainErrorString(short errNum)
  360. {
  361.     short i;
  362.     static char *UseFileName;
  363.  
  364.     if (ErrorAry == NULL) {
  365.     int fd;
  366.     short siz;
  367.     void *save;
  368.  
  369.     save = SetRequester((void *)-1);
  370.     UseFileName = ErrorFileName1;
  371.     fd = open(ErrorFileName1, O_RDONLY|O_BINARY);
  372.     SetRequester(save);
  373.  
  374.     if (fd < 0) {
  375.         if ((fd = open(ErrorFileName2, O_RDONLY|O_BINARY)) < 0) {
  376.         sprintf(ErrBuf, "(can't open %s!)", ErrorFileName2);
  377.         return(ErrBuf);
  378.         }
  379.         UseFileName = ErrorFileName2;
  380.     }
  381.     siz = lseek(fd, 0L, 2);
  382.     lseek(fd, 0L, 0);
  383.     ErrorAry = malloc(siz + 1);
  384.     read(fd, ErrorAry, siz);
  385.     close(fd);
  386.     {
  387.         char *ptr;
  388.         for (ptr = strchr(ErrorAry, '\n'); ptr; ptr = strchr(ptr + 1, '\n'))
  389.         *ptr = 0;
  390.     }
  391.     ErrorAry[siz] = 0;
  392.     ErrorArySize = siz;
  393.     }
  394.     for (i = 0; i < ErrorArySize; i += strlen(ErrorAry + i) + 1) {
  395.     char *ptr;
  396.     if (ErrorAry[i] == 'L' && ErrorAry[i+1] == 'K' && strtol(ErrorAry + i + 3, &ptr, 10) == errNum)
  397.         return(ptr + 1);
  398.     }
  399.     sprintf(ErrBuf, "(no entry in %s for error)", UseFileName ? UseFileName : "?");
  400.     return(ErrBuf);
  401. }
  402.  
  403. /*
  404.  *  Routines that do not exist in Lattice C
  405.  */
  406.  
  407. #ifndef _DCC
  408.  
  409. void *
  410. GetSucc(struct Node *node)
  411. {
  412.     struct Node *next = node->ln_Succ;
  413.  
  414.     if (next->ln_Succ == NULL)
  415.     next = NULL;
  416.     return((void *)next);
  417. }
  418.  
  419. void *
  420. GetTail(struct List *list)
  421. {
  422.     struct Node *node = list->lh_TailPred;
  423.  
  424.     if (node->ln_Pred == NULL)
  425.     node = NULL;
  426.     return((void *)node);
  427. }
  428.  
  429. void *
  430. GetHead(struct List *list)
  431. {
  432.     struct Node *node = list->lh_Head;
  433.  
  434.     if (node->ln_Succ == NULL)
  435.     node = NULL;
  436.     return((void *)node);
  437. }
  438.  
  439.  
  440. void *
  441. GetPred(struct Node *node)
  442. {
  443.     struct Node *pred = node->ln_Pred;
  444.  
  445.     if (pred->ln_Pred == NULL)
  446.     pred = NULL;
  447.     return((void *)pred);
  448. }
  449.  
  450. #endif
  451.  
  452. #ifdef AMIGA
  453.  
  454. void *
  455. SetRequester(void *new)
  456. {
  457.     void *old;
  458.     struct Process *proc = (struct Process *)FindTask(NULL);
  459.  
  460.     old = proc->pr_WindowPtr;
  461.     proc->pr_WindowPtr = new;
  462.     return(old);
  463. }
  464.  
  465. #else
  466.  
  467. void *
  468. SetRequester(void *new)
  469. {
  470.     return(NULL);
  471. }
  472.  
  473. #endif
  474.